package com.example.dangerouscargodriver.controller;

import android.content.Context;
import android.os.Message;
import android.util.Log;

import com.baiju.netmanager.Http.HttpRequest;
import com.baiju.netmanager.Http.IHttpRequest;
import com.baiju.netmanager.Http.INetResponse;
import com.example.dangerouscargodriver.DangerousCargoDriverApplication;
import com.example.dangerouscargodriver.entry.BankAccount;
import com.example.dangerouscargodriver.entry.PaySetInfo;
import com.example.dangerouscargodriver.event.Event;
import com.example.dangerouscargodriver.event.LoadHttpResourceStatus;
import com.example.dangerouscargodriver.module.ResponseInfo;
import com.example.dangerouscargodriver.param.RemoteAPI;
import com.example.dangerouscargodriver.param.RemoteAPICmd;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class PaySetInfoController extends Controller{

    public static final int EVENT_PAY_SET_LOADING = 0;
    public static final int EVENT_PAY_SET_LOAD_DONE = 1;

    private static final int MSG_LOAD_PAY_SET = 0;
    private static final  int MSG_ADD_LISTENER = 1;

    private static final String TAG = "PaySetInfoController";
    private static PaySetInfoController mInstance = null;

    private int mLoadHttpStatus = EVENT_PAY_SET_LOADING;

    private HttpRequest mHttp = null;
    private HttpResponseListener mHttpListener = null;

    private PaySetInfo mPaySetInfo = null;

    private HashMap<Class, IControllerEventListener> mControllerListener = null;

    public static PaySetInfoController Instance(){
        if(mInstance == null){
            mInstance = new PaySetInfoController(DangerousCargoDriverApplication.getAppContext());
        }

        return mInstance;
    }

    public PaySetInfoController(Context context){
        mHttpListener = new HttpResponseListener();
        mControllerListener = new HashMap<>();
        mHttp = HttpRequest.Instance(context);
        mHttp.attachResponseListener(mHttpListener, PaySetInfoController.class);
        mHttp.startUp();

        loadPaySetInfo();
    }

    public boolean attachControllerListener(Class object, IControllerEventListener listener){
        if(mControllerListener.containsKey(object)){
            return false;
        }
        else {
            mControllerListener.put(object, listener);
            Message msg = new Message();
            msg.what = MSG_ADD_LISTENER;
            msg.obj = object;
            sendMessage(msg);

            return true;
        }
    }

    public boolean unAttachControllerListener(Class object, IControllerEventListener listener){
        if(mControllerListener.containsKey(object) && mControllerListener.get(object) == listener){
            mControllerListener.remove(object);
            return true;
        }
        else {
            return false;
        }
    }

    private void loadPaySetInfo(){
        HashMap<String, String> header = new HashMap<String, String>();
        header.put("token", AuthController.Instance().getToken());
        header.put("app-type", "Android");

        mHttp.sendRequest(
                IHttpRequest.RequestMethod.GET_METHOD,
                RemoteAPI.REQUEST_V1_USER_PAY_SET,
                header,
                null,
                RemoteAPICmd.REQUEST_V1_USER_PAY_SET,
                PaySetInfoController.class);
    }

    @Override
    public void handleControllerMessage(Message msg){
        switch (msg.what){
            case MSG_LOAD_PAY_SET:
                mLoadHttpStatus = EVENT_PAY_SET_LOAD_DONE;
                for (Class key : mControllerListener.keySet()) {
                    LoadHttpResourceStatus status = new LoadHttpResourceStatus();
                    status.setWhat(mLoadHttpStatus);
                    mControllerListener.get(key).onEvent(Event.EVENT_LOAD_HTTP_PAY_SET, status.toJson());
                }
                break;
            case MSG_ADD_LISTENER:
                LoadHttpResourceStatus status = new LoadHttpResourceStatus();
                status.setWhat(mLoadHttpStatus);
                mControllerListener.get(msg.obj).onEvent(Event.EVENT_LOAD_HTTP_PAY_SET, status.toJson());
                break;
            default:
                break;
        }
    }

    private class HttpResponseListener implements INetResponse {

        private static final String TAG = "HttpResponseListener";

        @Override
        public void onResponse(int actionId, String response) {
            try {
                JSONObject res = new JSONObject(response);
                Gson gson = new Gson();
                if(actionId == RemoteAPICmd.REQUEST_V1_USER_PAY_SET){
                    Log.d(TAG, "onResponse: " + response);
                    ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
                    if(responseInfo != null && responseInfo.getStatus() == 1){
                        mPaySetInfo = gson.fromJson(res.getString("data"), PaySetInfo.class);
                    }
                    else{
                        //请求失败
                    }

                    Message msg = new Message();
                    msg.what = MSG_LOAD_PAY_SET;
                    sendMessage(msg);
                }
                else{

                }
            }
            catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onErrorResponse(int actionId, String response) {

        }
    }
}
